package com.yyge.vo;


/**
 * 对结果进行包装的类
 */
public class R<T>{
    // 代表状态码
    private Integer code;
    // 代表消息
    private String message;
    // 代表包装的数据
    private T data;
    public static final Integer SUCCESS=ResultCode.OK.getCode();
    public static final Integer FAILURE=ResultCode.FAILURE.getCode();
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
   public static <T> R<T> ok(){
      return reResult(SUCCESS,ResultCode.OK.getMessage(),null);
   }
   public static <T> R<T> ok(String message){
      return reResult(SUCCESS,message,null);
   }
   public static <T> R<T> ok(T data){
      return reResult(SUCCESS,null,data);
   }
   public static <T> R<T> ok(String message,T data){
      return reResult(SUCCESS,message,data);
   }
   public static <T> R<T> fail(){
      return reResult(FAILURE,ResultCode.FAILURE.getMessage(),null);
   }
   public static <T> R<T> fail(String message){
      return reResult(FAILURE,message,null);
   }
   public static <T> R<T> fail(T data){
      return reResult(FAILURE,null,data);
   }
   public static <T> R<T> fail(String message,T data){
      return reResult(FAILURE,message,data);
   }
    public static <T> R<T> reResult(int code,String message,T data){
        R<T> r=new R<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }



}
